package com.sk.bilibili.tool;

import com.sk.bilibili.service.MinioService;
import com.zaxxer.hikari.util.DriverDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author 邵康
 * @date 2021/12/8 12:03
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UpdateImage {

    @Autowired
    private MinioService minioService;
    private static final String JDBC_URL="jdbc:mysql://49.234.47.204:3306/bilibili?serverTimezone=GMT%2B8";
    private static final String DRIVER_CLASS="com.mysql.cj.jdbc.Driver";
    private static final String USER_NAME="root";
    private static final String PASSWORD="sk2233";

    @Test
    void updateImage(){
        JdbcTemplate template=createJdbcTemplate();
        List<String> list = minioService.list("res");
        template.update("delete from image");
        for (String url:list){
            template.update("insert into image(title, url, upload_date) values (?,?,?)",
                    url.substring(url.lastIndexOf('/')+1),url,new Date());
        }
    }

    private JdbcTemplate createJdbcTemplate() {
        DataSource dataSource=new DriverDataSource(JDBC_URL,DRIVER_CLASS,new Properties(),USER_NAME,PASSWORD);
        return new JdbcTemplate(dataSource);
    }

}
